package com.goods.controller.business;

import com.github.pagehelper.PageInfo;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/24 15:32
 * @description：物资来源
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/business/supplier")
public class SupplierWebController {
    @Autowired
    private SupplierService supplierService;

    //物资来源 分页
    @GetMapping("findSupplierList")
    //http://www.localhost:8989/business/supplier/findSupplierList?pageNum=1&pageSize=10&name=
    //@RequestParam String name,
    //                                         @RequestParam String contact,
    //                                         @RequestParam String address
    public ResponseBean findSupplierList(@RequestParam String pageNum, @RequestParam String pageSize,
                                         HttpServletRequest request){
        String name = request.getParameter("name");
        String contact = request.getParameter("contact");
        String address = request.getParameter("address");
        PageInfo<Supplier> page = supplierService.findSupplierList(pageNum,pageSize,name,contact,address);
        Map<String, Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getList());
        return ResponseBean.success(data);
    }

    //物资来源 添加
    //http://www.localhost:8989/business/supplier/add
    @PostMapping("add")
    public ResponseBean add(@RequestBody SupplierVO supplierVo){
        supplierService.add(supplierVo);
        return ResponseBean.success();
    }

    //物资来源 修改之 查单个
    @GetMapping("edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        SupplierVO supplierVo = supplierService.edit(id);
        return ResponseBean.success(supplierVo);
    }

    //物资来源 修改之 改单个
    @PutMapping("update/{id}")
    public ResponseBean update(@RequestBody SupplierVO supplierVo){
        supplierService.update(supplierVo);
        return ResponseBean.success();
    }

    //物资来源 删除
    @DeleteMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        supplierService.delete(id);
        return ResponseBean.success();
    }

    //物资入库 物资来源 查全部
    @GetMapping("findAll")
    public ResponseBean findAll(){
        PageInfo<Supplier> page = supplierService.findSupplierList(null,null,null,null,null);
        return ResponseBean.success(page.getList());
    }


}
